package com.ld.blecardlibrarydes.utils;


/**
 * Created by lirongkun on 16/10/21.
 */

public class DesUtil {
    public static String masterKey = "11121314151617188887868584838281";
    public static String encryptDes(String data){
        int length = data.length();
        StringBuffer newData = new StringBuffer();
        newData.append(data);
        int count = (16 - length%16)%16/2;
        if (count == 1){
            newData.append("00");
        }else if (count > 1){
            newData.append("80");
            for (int i = 0;i < count-1; i++){
                newData.append("00");
            }
        }
        StringBuffer desData = new StringBuffer();
        for (int j = 0; j < newData.length()/16; j++){
            desData.append(TripleDes.DES_3(newData.substring(16*j,16*j+16),masterKey,0));
        }
        return desData.toString();
    }
    public static String discryptDes(String data){
        StringBuffer newData = new StringBuffer();
        for (int i = 0; i < data.length()/16; i++){
            newData.append(TripleDes.DES_3(data.substring(16*i,16*i+16),masterKey,1));
        }
        return newData.toString();
    }
    public static void setMasterKey(String masterKey2){
        masterKey = masterKey2;
    }
}
